package com.iemes.controller.warehouse;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.inject.Inject;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import com.iemes.annotation.SystemLog;
import com.iemes.controller.index.BaseController;
import com.iemes.entity.HandworkFormMap;
import com.iemes.entity.UserFormMap;
import com.iemes.mapper.warehouse.HandworkMapper;
import com.iemes.plugin.PageView;
import com.iemes.util.Common;

@RequestMapping("/handworkproduct_form/")
@Controller
public class Handworkproduct_FormController extends BaseController {

	@Inject
	private HandworkMapper handworkMapper;

	// 查看手工入库表，显示按钮。
	@RequestMapping("handworkproduct_form")
	public String handworkproduct_form(Model model) {
		model.addAttribute("res", findByRes());
		return Common.BACKGROUND_PATH + "/warehouse/handworkproduct_form_list";
	}

	// 手工入库表分页
	@ResponseBody
	@RequestMapping("handworkproduct_form_findByPage")
	public PageView findByPage(String pageNow, String pageSize) throws Exception {
		HandworkFormMap handworkFormMap = getFormMap(HandworkFormMap.class);
		List<Map<String, Object>> handwork=handworkMapper.getAll(handworkFormMap);
		LuogicPaging(pageNow, pageSize, handwork);
		return pageView;
	}

	// 编辑手工入库表
	@RequestMapping("handworkproduct_form_edit")
	public String editUI(Model model) throws Exception {
		String id = getPara("id");
		if(Common.isNotEmpty(id)){
			//根据id获得手工入库表
			HandworkFormMap handwork=handworkMapper.getById(id);
			model.addAttribute("handwork",handwork);
		}
		return Common.BACKGROUND_PATH + "/warehouse/handworkproduct_form_edit";
	}

	// 修改手工入库表
	@ResponseBody
	@RequestMapping(value = "handworkproduct_form_editEntity")
	@Transactional(readOnly=false)//需要事务操作必须加入此注解
	@SystemLog(module = "手工入库表", methods = "查看手工入库表-修改手工入库表") // 凡需要处理业务逻辑的.都需要记录操作日志
	public String editEntity(HttpServletRequest request) throws Exception {
		HandworkFormMap handworkFormMap = getFormMap(HandworkFormMap.class);
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Date date = new Date();
		// 获取request
		request = ((ServletRequestAttributes)RequestContextHolder.getRequestAttributes()).getRequest();  
		// 通过工具类获取当前登录的bean
		UserFormMap userFormMap = (UserFormMap)Common.findUserSession(request);
		handworkFormMap.put("date", sdf.format(date));
		handworkFormMap.put("fillPeopleId", userFormMap.getInt("id"));
		handworkFormMap.put("status",1);
		baseMapper.editEntity(handworkFormMap);
		return "success";
	}
	
	// 新增手工入库表页面
	@RequestMapping("handworkproduct_form_add")
	public String addUI(Model model) throws Exception {
		return Common.BACKGROUND_PATH + "/warehouse/handworkproduct_form_add";
	}

	// 新增手工入库表操作
	@ResponseBody
	@RequestMapping(value = "handworkproduct_form_addEntity")
	@Transactional(readOnly=false)//需要事务操作必须加入此注解
	@SystemLog(module = "手工入库表", methods = "查看手工入库表-新增手工入库表") // 凡需要处理业务逻辑的.都需要记录操作日志
	public String addEntity(HttpServletRequest request) throws Exception {
		HandworkFormMap handworkFormMap = getFormMap(HandworkFormMap.class);
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Date date = new Date();
		// 获取request
		request = ((ServletRequestAttributes)RequestContextHolder.getRequestAttributes()).getRequest();  
		// 通过工具类获取当前登录的bean
		UserFormMap userFormMap = (UserFormMap)Common.findUserSession(request);
		handworkFormMap.put("date", sdf.format(date));
		handworkFormMap.put("fillPeopleId", userFormMap.getInt("id"));
		handworkFormMap.put("status",1);
		baseMapper.addEntity(handworkFormMap);
		return "success";
	}
	
	@ResponseBody
	@RequestMapping("handworkproduct_deleteEntity")
	@Transactional(readOnly=false)//需要事务操作必须加入此注解
	@SystemLog(module = "手工入库表", methods = "查看手工入库表-删除手工入库表")//凡需要处理业务逻辑的.都需要记录操作日志
	public String deleteEntity() throws Exception {
		HandworkFormMap handworkFormMap = getFormMap(HandworkFormMap.class);
		String[] ids = getParaValues("ids");
		String arr=ids[0];
		String[] str=arr.split(",");
		SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Date date=new Date();
		// 获取request
		HttpServletRequest request = ((ServletRequestAttributes)RequestContextHolder.getRequestAttributes()).getRequest();  
		// 通过工具类获取当前登录的bean
		UserFormMap userFormMap = (UserFormMap)Common.findUserSession(request);
		for(int i=0;i<str.length;i++){
			handworkFormMap.put("date", sdf.format(date));
			handworkFormMap.put("fillPeopleId", userFormMap.getInt("id"));
			handworkFormMap.put("status", 0);
			handworkFormMap.put("id", str[i]);
			handworkMapper.editEntity(handworkFormMap);
		}
		return "success";
	}
}












